import requests  # type: ignore
import json
import logging

logger = logging.getLogger("DailyInspection")

class FeishuNotifier:
    def __init__(self, webhook_url):
        self.webhook_url = webhook_url
        
    def send_message(self, content, is_alert=False):
        """发送飞书消息"""
        # 构建消息卡片
        card_content = {
            "msg_type": "interactive",
            "card": {
                "config": {
                    "wide_screen_mode": True
                },
                "header": {
                    "template": "red" if is_alert else "green",
                    "title": {
                        "content": "🚨 系统告警通知" if is_alert else "✅ 日常巡检报告",
                        "tag": "plain_text"
                    }
                },
                "elements": [
                    {
                        "tag": "div",
                        "text": {
                            "content": content,
                            "tag": "lark_md"
                        }
                    },
                    {
                        "tag": "hr"
                    },
                    {
                        "tag": "note",
                        "elements": [
                            {
                                "tag": "plain_text",
                                "content": "巡检系统自动发送"
                            }
                        ]
                    }
                ]
            }
        }
        
        try:
            response = requests.post(self.webhook_url, json=card_content)
            if response.status_code == 200:
                logger.info("飞书消息发送成功")
                return True
            else:
                logger.error(f"飞书消息发送失败: {response.text}")
                return False
        except Exception as e:
            logger.error(f"飞书消息发送异常: {e}")
            return False